import urllib.request as req


def create_request(page):
    start = (page-1)*20
    url = "https://movie.douban.com/j/chart/top_list?type=11&interval_id=100:90&action=&start=" + \
        str(start)+"&limit=20"
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26"
    }
    request = req.Request(url=url, headers=header)
    return request


def get_content(request):
    res = req.urlopen(request)
    con = res.read().decode("utf-8")
    return con


def down_load(page,con):
    with open(file="douban_"+str(page)+".json", mode="w", encoding="utf-8")as fp:
        fp.write(con)


if __name__ == '__main__':
    start_page = int(input("请输入起始页码"))
    end_page = int(input("请输入结束页码"))
    for page in range(start_page, end_page+1):
        print(page)
        request = create_request(page)
        con = get_content(request)
        down_load(page,con)
